var ysb={};
layui.config({
        base: layUiBasePath
    }).extend({
        treetable: '/statics/js/treetable'
    }).use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
        var token=localStorage.getToken;
        var renderTable=function(){
            // 渲染表格
            layer.load(4);
           
            treetable.render({
            	treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'menuId',
                treePidName: 'parentId',
                treeDefaultClose:true,
                treeLinkage: true,
                elem: '#auth-table',
                url: basePath+'/sys/menu/list?token='+token,
                page: true,
                cols: [[
                    {type: 'numbers'},
                    {field: 'name', minWidth: 100, title: '菜单/权限名称'},
                    {field: 'perms', title: '权限标识'},
                    {field: 'url', title: '菜单url'},
                    {field: 'icon', title: '菜单图标',templet:function(d){
                    	if(d.icon!=null&&d.icon!="null"){
                    		return '<i class="layui-icon">'+d.icon+'</i>';
                    	}else{
                    		return '';
                    	}
                    }},
                    {field: 'orderNum', width: 80, align: 'center', title: '排序号'},
                    {
                        field: 'type', width: 80, align: 'center', templet: function (d) {
                            if (d.type == 2) {
                                return '<span class="layui-badge layui-bg-gray">按钮</span>';
                            }else if (d.type == 0) {
                                return '<span class="layui-badge layui-bg-blue">目录</span>';
                            } else if (d.type == 1) {
                                return '<span class="layui-badge-rim">菜单</span>';
                            }
                        }, title: '类型'
                    },
                    {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        };
        //初始化加载
        renderTable();
        $('#btn-expand').click(function () {
            treetable.expandAll('#auth-table');
        });
        $('#btn-fold').click(function () {
            treetable.foldAll('#auth-table');
        });
        $('#btn-refresh').click(function () {
        	renderTable();
        });
        
        //监听工具条
        table.on('tool(auth-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if(data.id==null||data.id==""){
       		 layer.msg("菜单信息不存在，请刷新或者登陆后重试");
       		 return false;
          	}
            if (layEvent === 'del') {
            	layer.confirm('您真的要删除当前的数据吗？', {
      			  btn: ['确定','取消'] //按钮
      			}, function(){
      				ysb.delMenu(data.id);
      			});
            } else if (layEvent === 'edit') {
            	localStorage.getMenuId=data.id;
            }
        });
        
        //删除菜单
        ysb.delMenu=function(id){
            $.ajax({
            	type:"POST",
            	url:basePath+"/sys/menu/delete/"+id,
            	data:{"token":token},
	            success:function(data){
	            	var code=data.code;
	            	var msg=data.msg;
	            	if(code=="0"||msg=="success"){
	            		 layer.msg("数据操作成功");
	            		 renderTable();
	            	}else{
	            		   layer.msg(data.msg);
	            	}
	            }
            });
            return false;
        };
    });

function refreshed(){
	alert("11");
	renderTable();
}